package cn.edu.zjut.mina.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.zjut.mina.bean.RoomInfo;
import cn.edu.zjut.mina.interf.IRoomInfo;
import cn.edu.zjut.mina.po.TotalGameResult;
import cn.edu.zjut.mina.po.UserInfo;
import cn.edu.zjut.mina.repository.EveryGameResultRepository;
import cn.edu.zjut.mina.repository.PlayerBattleInfoRepository;
import cn.edu.zjut.mina.repository.TotalGameResultRepository;
import cn.edu.zjut.mina.repository.TotalRepository;
import cn.edu.zjut.mina.repository.UserInfoRepository;
import cn.edu.zjut.mina.service.IGameOverService;
import cn.edu.zjut.mina.util.RoomIdInfo;
import cn.edu.zjut.mina.util.UserSessionMap;

/**
 * 1对多关系，两边都需要set操作，否则保存后外键为null
 * @author 张宇翔
 *
 */

@Service("gameOverService")
public class GameOverServiceImpl implements IGameOverService{

	@Autowired
	private TotalGameResultRepository totalGameResultRepository;
	
	@Autowired
	private UserInfoRepository userInfoRepository;
	
//	@Autowired
//	private EveryGameResultRepository everyGameResultRepository;
//	@Autowired
//	private PlayerBattleInfoRepository playerBattleInfoRepository;
	
//	@Autowired
//	private TotalRepository totalRepository;
	
	/**
	 * 用于存放用户信息的集合
	 */
	private final UserSessionMap userSessionMap = UserSessionMap.getInstance();
	
	
	/**
	 * 游戏结果统计
	 */
	@Transactional(rollbackFor=Exception.class)
	public void statisticalResult(IRoomInfo roomInfo) {
		// TODO Auto-generated method stub
		roomInfo.getTotalGameResult().setActual_number_of_games(roomInfo.getActual_number_of_games());//实际游戏局数
		roomInfo.getTotalGameResult().setEndTime(new Date());//游戏结束时间
		
		
		
		totalGameResultRepository.save(roomInfo.getTotalGameResult());
//		for (int i = 0; i < totalGameResult.getEveryGameResult().size(); i++) {
//			everyGameResultRepository.save(totalGameResult.getEveryGameResult().get(i));
//			for (int j = 0; j < totalGameResult.getEveryGameResult().get(i).getPlayerBattleInfos().size(); j++) {
//				playerBattleInfoRepository.save(totalGameResult.getEveryGameResult().get(i).getPlayerBattleInfos().get(i));
//			}
//		}
		/**
		 * 数据库保存测试代码
		 */
//		Total total = new Total();
//		total.setEverys(new ArrayList<Every>());
//		Every every1 = new Every();
//		every1.setTotal(total);
//		every1.setUsers(new ArrayList<User>());
//		User user1 = new User();
//		user1.setUname("Test1");
//		user1.setEvery(every1);
//		User user2 = new User();
//		user2.setEvery(every1);
//		user2.setUname("Test2");
//		every1.getUsers().add(user1);
//		every1.getUsers().add(user2);
//		Every every2 = new Every();
//		every2.setTotal(total);
//		every2.setUsers(new ArrayList<User>());
//		User user3 = new User();
//		user3.setEvery(every2);
//		user3.setUname("Test3");
//		User user4 = new User();
//		user4.setEvery(every2);
//		user4.setUname("Test4");
//		every2.getUsers().add(user3);
//		every2.getUsers().add(user4);
//		total.getEverys().add(every1);
//		total.getEverys().add(every2);
//		totalRepository.save(total);
	}

	public void clearRoomInfo(IRoomInfo roomInfo) {
		// TODO Auto-generated method stub
		/**
		 * 下面清空房间数据与用户数据
		 */
		for (int i = 0; i < roomInfo.getActualParticipation(); i++) {
			UserInfo user = roomInfo.getUserSessions().get(i).user;
			user.clear();
		}
		userSessionMap.remove(roomInfo.getRoomId());
		RoomIdInfo.giveBack(roomInfo.getRoomId());
	}

}
